package com.kuang.gulimall.member.controller;

import com.kuang.common.exception.BizCodeEnume;
import com.kuang.common.utils.PageUtils;
import com.kuang.common.utils.R;
import com.kuang.common.vo.MemberRespVo;
import com.kuang.gulimall.member.entity.MemberEntity;
import com.kuang.gulimall.member.execption.PhoneExistException;
import com.kuang.gulimall.member.execption.UsernameExistException;
import com.kuang.gulimall.member.feign.CouponFeign;
import com.kuang.gulimall.member.service.MemberService;
import com.kuang.gulimall.member.vo.MemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author xiangmou
 * @email 1140968804@qq.com
 * @date 2021-05-05 19:55:41
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    //测试我们的feign有没有成功
    @Autowired
    CouponFeign couponFeign;

    //远程登录
    @PostMapping("/login")
    public R login(@RequestBody MemberRespVo vo){

        //登录
        MemberRespVo login = memberService.login(vo);
        if (login!=null){
            //登录成功
            return R.ok().put("loginVo",login);
        }else return R.error(BizCodeEnume.LOGIN_ERROR_EXCEPTION.getCode(),BizCodeEnume.LOGIN_ERROR_EXCEPTION.getMsg());

    }

    //远程注册模块服务
    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo vo){

        //去重需要进行try、catch去异常处理
        try {
            memberService.regist(vo);
        } catch (PhoneExistException e) {
            return R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        } catch (UsernameExistException e) {
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(),BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        }

        return R.ok();
    }


    @RequestMapping("/coupons")
    public R test() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        R membercoupons = couponFeign.membercoupons();
        return R.ok().put("member", memberEntity).put("coupons", membercoupons.get("coupons"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
